<?
/***********************************************************************************************
Created by : G.M.Sundar 
[created : 02/07/2007, 04:44:47 PM]
 Description:

/***********************************************************************************************/

include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/global_form.inc");//
include(DOCINCLUDES."global_files/global_report.inc");
include(DOCINCLUDES."global_files/tables.inc");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
 include(DOCINCLUDES."global_files/global_session.inc");


$page=new cPage;
$page->header_path="../../";

$form=new cForm();
$db=new cDatabase($company_id);
$report=new cReport();
$sql=new pgSql();//

if($_GET)
{
$account=$_GET['account'];
$buyer=$_GET['buyer'];
$action=$_GET['action'];
if($action=="edit")
$commercial_id=$_GET['commercial_id'];
}
else
{
$action=$_POST['action'];
if($action=="edit")
$commercial_id=$_POST['commercial_id'];

}
if($_POST)

for($i=1;$i<=15;$i++)
 {

	if($_POST['item_desc_'.$i])
	{
		
		$data_array[$i]['item_desc']=$_POST['item_desc_'.$i];
		$data_array[$i]['remarks']=$_POST['remarks_'.$i];
		$data_array[$i]['qty']=$_POST['qty_'.$i];
		$data_array[$i]['val']=$_POST['val_'.$i];
		$data_array[$i]['item_id']=$_POST['item_id_'.$i];
	}
 }

if(count($data_array)>=1)
	{

$db->beginTransaction();
 
 	$coo_commercial_array["buyer_id"]=$_POST['buyer'];
 	$coo_commercial_array["account_id"]=$_POST['account'];
	$coo_commercial_array["pre_carriage"]=$_POST['pre_carriage_'];
	$coo_commercial_array["place_of_receipt"]=$_POST['place_of_receipt_'];
	if($_POST['ship_mode_'])
	$coo_commercial_array["ship_mode_id"]=$_POST['ship_mode_'];
	if($_POST['city_from_'])
	$coo_commercial_array["ship_from_city"]=$_POST['city_from_'];
	if($_POST['port_dis_'])
	$coo_commercial_array["port_dis"]=$_POST['port_dis_'];
	if($_POST['port_des_'])
	$coo_commercial_array["port_des"]=$_POST['port_des_'];
	$coo_commercial_array["marks_and_nos"]=$_POST['marks_and_nos_'];
	$coo_commercial_array["no_of_pkgs"]=$_POST['kind_of_pkgs_'];
	$coo_commercial_array["description"]=$_POST['description_'];
	$coo_commercial_array["quantity_type_id"]=$_POST['qty_'];//
	$coo_commercial_array["value"]=$_POST['val_'];//
	$coo_commercial_array["name_of_signatory"]=$_POST['sign_name_'];//
	$coo_commercial_array["total_qty"]=$_POST['total_qty'];//
	$coo_commercial_array["total_val"]=$_POST['total_val'];//
	$coo_commercial_array["invoice_no"]=$_POST['invoice_no'];
	$coo_commercial_array["invoice_date"]=getFormattedDate($_POST['invoice_date']);
	$sql->table = $coo_commercial_details_table;
	$sql->column = $coo_commercial_array;

	if($action=='edit')
		{
		$condition_array['commercial_id']="commercial_id=".$commercial_id;
  $sql->condition =$condition_array;
  $sql->generateUpdateQuery();
		}
	else
		{
		$sql->generateInsertQuery();
		}

	 $db->query = $sql->query;
	 $db->runQuery();
       if($action!='edit')
		{
		$db->query="Select max(commercial_id) as commercial_id from
	$coo_commercial_details_table";
		$db->runQuery();
		$id_array=$db->returnArrays();
		$commercial_id=$id_array[0]['commercial_id'];
		}
	$item_no=1;
	
 	foreach($data_array as $key=>$value)
		{
		$coo_item_details_array["commercial_id"]=$commercial_id;
		$coo_item_details_array["item_no"]=$item_no;
		$coo_item_details_array["item_desc"]=$value['item_desc'];
		$coo_item_details_array["remarks"]=$value['remarks'];
		$coo_item_details_array["qty"]=$value['qty'];
		$coo_item_details_array["val"]=$value['val'];

		$sql->table = $coo_item_details_table;
		$sql->column = $coo_item_details_array;

       if($action=='edit')
		{
		$condition_array['item_id']="item_id=".$value['item_id'];
		$sql->condition =$condition_array;
		$sql->generateUpdateQuery();
		}
	else
		{
		$sql->generateInsertQuery();
		}
		$db->query = $sql->query;
		$db->runQuery();
		$item_no++;
		}
$db->endTransaction();
unset($_POST);
	header("location:certificate_of_origin_details.php?commercial_id=$commercial_id");
exit;
	}

if($action=="edit")
$title="Edit Page for Certificate of Origin no - $commercial_id";
else
$title="Certificate of Origin Entry Page";

$page->title=$page->heading="$title";
$page->doHeaderHtml();


$form->properties=array("name"=>"cooform","method"=>"post","action"=>$_SERVER
["PHP_SELF"]);
echo $form->startForm();
$coo_commercial_details_array=array();
 if($action=='edit')
{

$db->query="SELECT buyer_id,account_id,pre_carriage,place_of_receipt,ship_mode_id,ship_from_city,port_dis,port_des,marks_and_nos,no_of_pkgs,description,quantity_type_id,value,name_of_signatory from $coo_commercial_details_table where commercial_id =$commercial_id ";
$db->runQuery();
$coo_commercial_details_array=$db->returnArrays();


$account=$coo_commercial_details_array[0]['account_id'];

$buyer=$coo_commercial_details_array[0]['buyer_id'];
}

$db->query="SELECT account_name,1,concat(company_name,'-',account_code) as company_name from $_jaccount_table ja join
$_mcompany_table mc on mc.company_id=ja.account_name order by
3;";
$db->runQuery();
$account_array=$db->returnArrays();


$form->properties=array("name"=>"account","class"=>"textm","onChange"=>"self.
location.href=\"certificate_of_origin.php?account=\"+this.value");
	$form->select_tag_properties=array("default"=>"$account","value"=>$_GET[
"account"],"arrays"=>$account_array);
$cooform_data[0]["Exporter Name"]=$form->generateSelectTag();


if($account)
{

 $db->query="SELECT company_name as \"display_exporter\",
address,cc.city_name
as
\"city_id\",cc.country_name
as countryfrom,mc.city_name,ms.state_name,mco.country_name,zipcode from
$company_contact_table cc left join $_mcity_table mc on mc.city_id=cc.city_name left join
$_mcountry_table mco on mco.country_id=cc.country_name left join $_mstate_table ms on
ms.state_id=cc.state where company_id =$account";	
$db->runQuery();
$account_contact_array=$db->returnArrays();

$cooform_data[0]["ExporterName"]=$account_contact_array[0]['display_exporter'];
$cooform_data[0]["Address"]=$account_contact_array[0]['address'];
$cooform_data[0]["City"]=$account_contact_array[0]['city_name'];
$cooform_data[0]["State"]=$account_contact_array[0]['state_name'];
$cooform_data[0]["Country"]=$account_contact_array[0]['country_name'];
$cooform_data[0]["Zip code"]=$account_contact_array[0]['zipcode'];
$cooform_data[0]["Address"]=$account_contact_array[0]['address'];




if($action=='edit')
{
$buyer=$coo_commercial_details_array[0]['buyer_id'];
}

$db->query="SELECT buyer_name,1,concat(company_name,'-',buyer_code) as company_name from $_jbuyer_table jb join
$_mcompany_table mc on mc.company_id=jb.buyer_name order by 3;";
$db->runQuery();
$buyer_array=$db->returnArrays();

$form->properties=array("name"=>"buyer","class"=>"textm","onChange"=>"self.
location.href=\"certificate_of_origin.php?account=$account&buyer=\"+this.value");
	$form->select_tag_properties=array("default"=>"$buyer","value"=>$buyer,"arrays"=>$buyer_array);
$cooform_data[0]["Buyer"]=$form->generateSelectTag();

if($buyer)
{
 $db->query="SELECT company_name as \"display_buyer\",
address,cc.country_name as countryto,cc.city_name
as \"city_id\",mc.city_name,ms.state_name,mco.country_name,zipcode
from $company_contact_table cc left join $_mcity_table mc on mc.city_id=cc.city_name
left join $_mcountry_table mco on mco.country_id=cc.country_name left join $_mstate_table
ms on ms.state_id=cc.state where company_id = $buyer ";		
$db->runQuery();
$buyer_contact_array=$db->returnArrays();

$cooform_data[0]["BuyerName"]=$buyer_contact_array[0]['display_buyer'];
$cooform_data[0]["Buyer Address"]=$buyer_contact_array[0]['address'];
$cooform_data[0]["Buyer City"]=$buyer_contact_array[0]['city_name'];
$cooform_data[0]["Buyer State"]=$buyer_contact_array[0]['state_name'];
$cooform_data[0]["Buyer Country"]=$buyer_contact_array[0]['country_name'];
$cooform_data[0]["Buyer Zip code"]=$buyer_contact_array[0]['zipcode'];
$cooform_data[0]["Buyer Address"]=$buyer_contact_array[0]['address'];


if($action=='edit')
$pre_carriage=$coo_commercial_details_array[0]['pre_carriage'];
else
$pre_carriage=$coo_commercial_details_array[0]['pre_carriage']?$coo_commercial_details_array[0]['origin_requirements']:"";
$form->properties =
array("name"=>"pre_carriage_","value"=>$pre_carriage,"size"=>"50",
"maxlength"=>"250","class"=>"textn");
$cooform_data[0]["Pre Carriage By"]=$form->generateInputTag();
$coo_item_details_array=array();
if($action=='edit')
$place_of_receipt=$coo_commercial_details_array[0]['place_of_receipt'];
else
$place_of_receipt=$coo_commercial_details_array[0]['place_of_receipt']?$coo_commercial_details_array[0]['place_of_receipt']:"";
$form->properties =
array("name"=>"place_of_receipt_","value"=>$place_of_receipt,"size"=>"50",
"maxlength"=>"350","class"=>"textn","title"=>"Use '~' to split book nos in the sequence they are used");
$cooform_data[0]["Place of Receipt"]=$form->generateInputTag();

if($action=='edit')
$ship_mode_id=$coo_commercial_details_array[0]['ship_mode_id'];
else
$ship_mode_id=$coo_commercial_details_array[0]['ship_mode_id']?$coo_commercial_details_array[0]['ship_mode_id']:"";


$db->query="SELECT shipment_mode_id,1,shipment_mode_name from
$_mship_mode_table order by 3;";
$db->runQuery();
$ship_mode_array=$db->returnArrays();

$form->properties=array("name"=>"ship_mode_");
$form->select_tag_properties=array("default"=>$ship_mode_id,"arrays"=>$ship_mode_array,"restore_default"=>$_POST[
"ship_mode"]);
$cooform_data[0]["Ship Mode"]=$form->generateSelectTag();


if($action=='edit')
$ship_from_city=$coo_commercial_details_array[0]['ship_from_city'];
else
$ship_from_city=$coo_commercial_details_array[0]['ship_from_city']?$coo_commercial_details_array[0]['ship_from_city']:"";

$db->query="SELECT city_id,1,city_name from $master_city_table order by 3;";
$db->runQuery();
$location_array=$db->returnArrays();

$form->properties=array("name"=>"city_from_");
$form->select_tag_properties=array("default"=>$ship_from_city,"arrays"=>$location_array,"restore_default"=>$_POST[
"city_from"]);
$cooform_data[0]["City From"]=$form->generateSelectTag();

if($action=='edit')
$port_dis=$coo_commercial_details_array[0]['port_dis'];
else
$port_dis=$coo_commercial_details_array[0]['port_dis']?$coo_commercial_details_array[0]['port_dis']:"";


$db->query="SELECT port_id,1,port_name from $_mport_table order by 3;";
$db->runQuery();
$port_array=$db->returnArrays();

if($action=='edit')
$port_of_discharge=$coo_commercial_details_array[0]['port_of_discharge'];
else
$form->properties=array("name"=>"port_dis_");
$form->select_tag_properties=array("default"=>$port_dis,"arrays"=>$port_array,"restore_default"=>$_POST[
"port_dis"]);
$cooform_data[0]["Port of Discharge"]=$form->generateSelectTag();

if($action=='edit')
$port_des=$coo_commercial_details_array[0]['port_des'];
else
$port_des=$coo_commercial_details_array[0]['port_des']?$coo_commercial_details_array[0]['port_des']:"";

$db->query="SELECT port_id,1,port_name from $_mport_table order by 3;";
$db->runQuery();
$port_array=$db->returnArrays();

$form->properties=array("name"=>"port_des_");
$form->select_tag_properties=array("default"=>$port_des,"arrays"=>$port_array,"restore_default"=>$_POST[
"port_des"]);
$cooform_data[0]["Final Destination"]=$form->generateSelectTag();

if($action=='edit')
$marks_and_nos=$coo_commercial_details_array[0]['marks_and_nos'];
else
$marks_and_nos=$coo_commercial_details_array[0]['marks_and_nos']?$coo_commercial_details_array[0]['marks_and_nos']:"";

$form->properties =
array("name"=>"marks_and_nos_","value"=>$marks_and_nos,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$cooform_data[0]["Marks & Nos."]=$form->generateInputTag();

if($action=='edit')
$no_of_pkgs=$coo_commercial_details_array[0]['no_of_pkgs'];
else
$no_of_pkgs=$coo_commercial_details_array[0]['no_of_pkgs']?$coo_commercial_details_array[0]['no_of_pkgs']:"";

$form->properties =
array("name"=>"kind_of_pkgs_","value"=>$no_of_pkgs,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$cooform_data[0]["No. and Kind of Packages"]=$form->generateInputTag();

if($action=='edit')
$description=$coo_commercial_details_array[0]['description'];
else
$description=$coo_commercial_details_array[0]['description']?$coo_commercial_details_array[0]['description']:"";


$form->properties =
array("name"=>"description_","value"=>$description,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$cooform_data[0]["Description of Goods"]=$form->generateInputTag();

if($action=='edit')
$quantity_type_id=$coo_commercial_details_array[0]['quantity_type_id'];
else
$quantity_type_id=$coo_commercial_details_array[0]['quantity_type_id']?$coo_commercial_details_array[0]['Quantity Type']:"";

$form->properties =
array("name"=>"qty_","value"=>$quantity_type_id,"size"=>"20",
"maxlength"=>"10","class"=>"textn");
$cooform_data[0]["Quantity"]=$form->generateInputTag();

if($action=='edit')
$value=$coo_commercial_details_array[0]['value'];
else
$value=$coo_commercial_details_array[0]['value']?$coo_commercial_details_array[0]['Value']:"";

$form->properties =
array("name"=>"val_","value"=>$value,"size"=>"20",
"maxlength"=>"10","class"=>"textn");
$cooform_data[0]["Value"]=$form->generateInputTag();

if($action=='edit')
$name_of_signatory=$coo_commercial_details_array[0]['name_of_signatory'];
else
$name_of_signatory=$coo_commercial_details_array[0]['name_of_signatory']?$coo_commercial_details_array[0]['name_of_signatory']:"";

$form->properties =
array("name"=>"sign_name_","value"=>$name_of_signatory,"size"=>"20",
"maxlength"=>"350","class"=>"textn");
$cooform_data[0]["Name of the Signatory"]=$form->generateInputTag();

if($action=='edit')
$invoice_no=$coo_commercial_details_array[0]['invoice_no'];
else
$invoice_no=$coo_commercial_details_array[0]['invoice_no']?$coo_commercial_details_array[0]['invoice_no']:"";

$form->properties =
array("name"=>"invoice_no","value"=>$invoice_no,"size"=>"20",
"maxlength"=>"350","class"=>"textn");
$cooform_data[0]["Invoice Number"]=$form->generateInputTag();

$now=date("d/m/Y");
$_POST["invoice_date"]=($brc_commercial_details_array[0]['invoice_date'])?$brc_commercial_details_array[0]['invoice_date']:$now;

$form->properties=array("type"=>"text","name"=>"invoice_date","value"=>
$_POST["invoice_date"],"size"=>"9","maxlength"=>"10","class"=>"datem");
$cooform_data[0]["Date (dd/mm/yyyy)"]=$form->generateInputTag();

$cooform_data[0]["Date (dd/mm/yyyy)"].="<a
href=\"javascript:show_calendar('cooform.invoice_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"".$page->header_path."images/calendar.gif\" width=\"16\"
height=\"16\" border=\"0\"></a>";
$cooform_data[0]["Date (dd/mm/yyyy)"].=$dv->errors["issue_date"];

$coo_item_details_array=array();
 if($action=='edit')
{
$db->query="SELECT
item_id,commercial_id,item_no,item_desc,remarks,qty,val
from $coo_item_details_table where commercial_id =$commercial_id ";
$db->runQuery();
$coo_items_array=$db->returnArrays();
}

$j=1;
$no_of_lines=15;
for($i=1;$i<=$no_of_lines;$i++)
{

$item_desc[$i]=$coo_items_array[$i-1]['item_desc']?$coo_items_array[$i-1]['item_desc']:$_POST['item_desc_'.$i];

$form->properties =
array("name"=>"item_desc_".$i,"value"=>$item_desc[$i],"size"=>"20","class"=>
"textn","TABINDEX"=>"$j");
$cooitem_data[0]["Substance and colour"].=$form->generateInputTag();
$j++;

$remarks[$i]=$coo_items_array[$i-1]['remarks']?$coo_items_array[$i-1]['remarks']:$_POST['remarks_'.$i];

$form->properties =
array("name"=>"remarks_".$i,"value"=>$remarks[$i],"size"=>"20", "maxlength"=>"5","class"=>"textn","TABINDEX"=>"$j");
$cooitem_data[0]["Remarks"].= $form->generateInputTag();
$j++;


$qty[$i]=$coo_items_array[$i-1]['qty']?$coo_items_array[$i-1]['qty']:$_POST['qty_'.$i];
$form->properties =
array("name"=>"qty_".$i,"value"=>$qty[$i],"size"=>"20","class"=>
"textn","TABINDEX"=>"$j","onChange"=>"calculateqty($no_of_lines);");
$cooitem_data[0]["Quantity"].=
$form->generateInputTag();
$j++;

$val[$i]=$coo_items_array[$i-1]['val']?$coo_items_array[$i-1]['val']:$_POST['val_'.$i];
$form->properties =
array("name"=>"val_".$i,"value"=>$val[$i],"size"=>"20","class"=>
"textn","TABINDEX"=>"$j","onChange"=>"calculateval($no_of_lines);");
$cooitem_data[0]["Value"].=
$form->generateInputTag();
$j++;

$form->properties=array("type"=>"hidden","name"=>"item_id_$i","value"=>$coo_items_array[$i-1]['item_id']);
echo $form->generateInputTag();


}


if($action=='edit')
$button_text="Edit";
else
$button_text="Insert";
$form->properties=array("type"=>"button","name"=>"insert","value"=>"$button_text",
"onClick"=>"showConfirmBox('certificate_of_origin.php','certificate_of_originform');");
$coo_save[0][""]="<BR>".$form->generateInputTag();


$form->properties=array("type"=>"hidden","name"=>"commercial_id","value"=>"$commercial_id");
echo $form->generateInputTag();
$form->properties=array("type"=>"hidden","name"=>"action","value"=>"$action");
echo $form->generateInputTag();

}
}
$report->data=$cooform_data;
$report->title="$title";
$report->customization["table_style"]="width:40%;";
echo "<div align=\"center\">";
$report->doVerticalTable();



$report->data=$cooitem_data;
$report->title="";
$report->customization["table_style"]="width:50%;
backcolour:white;border-style:none; border-width:none;
border-collapse:collapse";
$report->customization["tr_style"]="border-style:none;
border-width:thin; border-collapse:collapse";
$report->customization["column_style"][""]="text-align:center";
$report->highlight=true;
$report->doHorizontalTable();
echo "</div>";

if($buyer)
{
//***************************************************************
for($nb=0;$nb<=167;$nb++)
echo "&nbsp;";

$form->properties =
array("name"=>"total_qty","value"=>0,"size"=>"20","class"=>
"textm");
echo $form->generateInputTag();
for($nb=0;$nb<=4;$nb++)
echo "&nbsp;";
$form->properties =
array("name"=>"total_val","value"=>0,"size"=>"20","class"=>
"textm");
echo $form->generateInputTag();
//***************************************************************
}
$report->data=$coo_save;
$report->title="";
$report->customization["table_style"]="width:100%;
backcolour:white;border-style:none; border-width:none;
border-collapse:collapse";
$report->customization["tr_style"]="border-style:none;
border-width:none";

$report->highlight=False;
$report->doHorizontalTable();
echo $form->endForm();

?>


<script language="javascript">

function showConfirmBox(file_name,form_name)
{
var ok = confirm("Do you want to save");
if(ok)
{
document.cooform.action="certificate_of_origin.php";
document.cooform.submit();
}
}

function calculateqty(nos)
{
var totalvalue=0;
if(nos=="")
nos=15;

for(var i=1;i<=nos;i++)
{
var currentval=parseFloat(document.cooform.eval("qty_"+i).value)
if(!isNaN(currentval))
totalvalue=totalvalue+currentval;
}

document.cooform.total_qty.value=totalvalue.toFixed(2);
}

function calculateval(nos)
{
var totalvalue=0;
if(nos=="")
nos=15;

for(var i=1;i<=nos;i++)
{
var currentval=parseFloat(document.cooform.eval("val_"+i).value)
if(!isNaN(currentval))
totalvalue=totalvalue+currentval;
}

document.cooform.total_val.value=totalvalue.toFixed(2);
}

</script>
